package day8;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @author oldliu
 * @since 1.0
 */
public class MyServer {
    public static void main(String[] args) {
        try {
            ServerSocket st=new ServerSocket(8888);
            System.out.println("服务启动了");
            while (true){
                //建立连接
                Socket s = st.accept();
                System.out.println(s+"连接了");
                InputStream in = s.getInputStream();
                OutputStream out = s.getOutputStream();
                BufferedReader br=new BufferedReader(new InputStreamReader(in));
                PrintWriter pw=new PrintWriter(out,true);//
                //聊天，直到碰到quit就停
                String words=null;
                //回复 知道了
                while (true){
                    words=br.readLine();

                    if(words.length()==0)
                        continue;
                    System.out.println("客户端说："+words);
                    pw.println("你说的问题："+words+",朕知道了");
                    //pw.flush();
                    if("quit".equals(words))
                        break;
                }
                s.close();
                System.out.println(s+"退出了");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
